<template>
  <div>
    <page-search :search-form-config="searchFormConfig" />
    <page-content
      :content-table-config="contentTableConfig"
      page-name="role"
      @createBtnClick="handleCreateClick"
      @editBtnClick="handleEditClick"
    ></page-content>
    <page-modal
      ref="pageModalRef"
      :modal-form-config="modalFormConfig"
      :defaultInfo="defaultInfo"
      page-name="role"
      :other-info="otherInfo"
    >
      <div>
        <el-tree
          ref="elTreeRef"
          :data="menuData"
          show-checkbox
          node-key="id"
          highlight-current
          :props="{ children: 'children', label: 'name' }"
          @check="handleCheckClick"
        />
      </div>
    </page-modal>
  </div>
</template>

<script setup lang="ts">
import PageSearch from '@/components/page-search/src/page-search.vue'
import { searchFormConfig } from './config/searchFormConfig'

import PageContent from '@/components/page-content'
import { contentTableConfig } from './config/contentTableConfig'

import PageModal from '@/components/page-modal'
import { modalFormConfig } from './config/modalFormConfig'
import { usePageModal } from '@/hooks/usePageModal'
import { menuMapLeafKeys } from '@/utils/map-menus'

import { computed, nextTick, ref } from 'vue'
import { ElTree } from 'element-plus'
//点击编辑时的树结构回显数据
const elTreeRef = ref<InstanceType<typeof ElTree>>()
const editCb = (item) => {
  const leafKeys = menuMapLeafKeys(item.menuList)
  //nextTick防止elTree上的value还没有绑定
  nextTick(() => {
    console.log(elTreeRef.value)
    elTreeRef.value?.setCheckedKeys(leafKeys, false)
  })
}
const [pageModalRef, defaultInfo, handleCreateClick, handleEditClick] =
  usePageModal(undefined, editCb)

//选择树结构数据时更新menuList，供发送网络请求使用
import { useStore } from '@/store'

const store = useStore()
const otherInfo = ref({})
const menuData = computed(() => store.state.entireMenu)
const handleCheckClick = (data1: any, data2: any) => {
  const checkedKeys = data2.checkedKeys
  const halfCheckedKeys = data2.halfCheckedKeys
  const menuList = [...checkedKeys, ...halfCheckedKeys]
  otherInfo.value = { menuList }
  console.log(menuList)
}
</script>

<style scoped></style>
